#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPainter>
#include <QPaintEvent>
// serial port
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QDateTime>

#include <QStackedWidget>

// bootloader
#include <QDir>
#include <QFileDialog>
#include <QTimer>
#include <QDateTime>

QT_BEGIN_NAMESPACE


namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void sendPacket(const QByteArray &data);
    void dtu_send_command_packet(unsigned short frame_type); // 串口发送函数
    void dtu_ReadFile_packet(unsigned short frame_type,int FileIdex);  // 读取文件函数

private slots:
    // serial port
    void on_openSerialButton_clicked();
    void showupdateData();
    void updateSerialPortList();
    void on_RfSerialButton_clicked();
    void DataReady();
    void appendDataToTextEdit(const QByteArray &data);
    void on_ClearSerialButton_rx_clicked();
    void on_SaveSerialButton_rx_clicked();
    void on_XTY_zd60test_show_clicked();
    void on_chart_show_clicked();


    void timer_callback();
    void on_openButton_clicked();
    void on_button_start_iap_clicked();
    void on_pushButton_serial_clicked();
    void on_pushButton_power_show_clicked();
private:
    void set_ui_type(void);
    QByteArray receivedData;
    bool isSerialPortOpen = false;  // 串口是否打开索引
    bool isSerialPortRec = false;  // 串口是否有数据接收
    bool isFineOpen = false;  // 文件是否打开索引
    int SerialPort_RecNum = 0;
    // bootloader
    QTimer *timer;
    QByteArray hexData;
    int ResendTimeNum = 0;
    int packetSize = 512;   // 有效数据包大小
    int numPackets;
    int currentPacket;
    unsigned char update_status = 0;
    Ui::MainWindow *ui;
    void openHexFile();
    void sendUpdateCommandPacket();
    void sendUpdatePromPacket(int packetNumber);
    QByteArray preparePacket(unsigned short frame_type,int packetNumber);
    QString PritfQByteArray(QByteArray aData);
    void onDataReceived();
    unsigned short chooseBoard();  // 升级板子选择
    void serialPort_callback();

};

extern QSerialPort *serialPort;
// private:

// void set_ui_show(QMainWindow *MainWindow);
// void Initialize_display();
// Ui::MainWindow *ui;
// QTimer *timer;
// };
#endif // MAINWINDOW_H
